/**
 * 小程序公司控制器
 */

class AppCompanyController extends BasicController {

    /**
     * 新增公司
     * @return {[type]} [description]
     */
    async actionCreate() {
        let { building_id, name, room_ids } = $_POST;
        let UID = this.state.user ? this.state.user.id : 0;
        let check = await this.modules('Company').where({ building_id, name }).first();
        if (check) {
            throw new Error('公司已存在');
        }
        let userInfo = null;
        let accountInfo = null;
        if (UID) {
            userInfo = await this.modules('User').where({id:UID}).first();
            if (!userInfo) {
                throw new Error('用户不存在');
            }
            if (!userInfo.mobile || !userInfo.id_card) {
                throw new Error('请先绑定手机和身份证');
            }
            accountInfo = await this.modules('Account').where({mobile:userInfo.mobile,id_card:userInfo.id_card}).first();
            if (accountInfo && accountInfo.company_id) {
                throw new Error('请勿重复创建公司');
            }
        }
        let t = await db.transaction();
        try {
            let roomWhere = null;
            if (room_ids && room_ids.length) {
                roomWhere = { building_id, company_id: 0 };
                roomWhere['id'] = {
                    [Op.in]: room_ids
                }
            }
            delete $_POST['room_ids'];
            let data = await this.modules('Company').insert($_POST, { transaction: t });
            if (data) {
                if (roomWhere) {
                    let res = await this.modules('Building.Room').where(roomWhere).update({ company_id: data.id }, { transaction: t });
                    if (!res) {
                        throw new Error('保存失败');
                    }
                }
                if (userInfo) {
                    if (accountInfo) {
                        let accountRes = await this.modules('Account').where({id:accountInfo.id}).update({ company_id: data.id }, { transaction: t });
                        if (!accountRes) {
                            throw new Error('保存失败');
                        }
                    } else {
                        let accountData = {
                            company_id:data.id,
                            user_name:userInfo.mobile,
                            mobile:userInfo.mobile,
                            real_name:userInfo.real_name,
                            id_card:userInfo.id_card,
                            app_id:3,
                            is_disable:0,
                            is_admin:0
                        }
                        let accountRes = await this.modules("Account").insert(accountData, { transaction: t });
                        if (!accountRes) {
                            throw new Error('保存失败');
                        }
                    }
                }
                await t.commit();
                return data;
            } else {
                throw new Error('保存失败');
            }
        } catch (error) {
            await t.rollback();
            throw error;
        }
    }

    /**
     * 公司详情
     * @return {[type]} [description]
     */
    async actionView() {
        let company = await this.modules('Company').where({ id: $_POST['company_id'] }).first();
        if (!company) {
            throw new Error('公司不存在');
        }
        return company;
    }

}

export default AppCompanyController;